﻿using LPBToolsLib;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;

namespace StationSer
{
    internal class APIServer
    {
        public StationsServer tStaServer { get;protected  set; } = null;

        public Recorder_Error SysErr { get; set; } = null;

        public Recorder_RunLog RunLog { get; set; } = null;

        public bool runWithJSON(string tJSONCommands, out string tResponJSONInfos)
        {
            tResponJSONInfos = "";
            Dictionary<string, object> tCommands = null;
            try
            {
                tCommands = JsonConvert.DeserializeObject<Dictionary<string, object>>(tJSONCommands);
                
                if ((tCommands != null) && (tCommands.Count > 0))
                {
                    Dictionary<string, object> tResponInfos = new Dictionary<string, object>();
                    if (runWithJSON(tCommands, tResponInfos))
                    {
                        tResponJSONInfos = JsonConvert.SerializeObject(tResponInfos);
                        return true;
                    }
                }
            }
            catch (Exception ex)
            {
                SysErr?.setRecord(ex);
            }
            return false;
        }

        public bool runWithJSON(Dictionary<string, object> tCommands, Dictionary<string, object> tResponInfos)
        {
            int iResult = 0;
            if ((tStaServer != null) && (tResponInfos != null))
            {
                tResponInfos.Clear();
                //tResponInfos.Add("Msg", "OK");
                //tResponInfos.Add("Error", 0);
                int iValue = 0;
                iResult = 1;


                IFDExCommunicater tCommunicater = null;
                if (tCommands.ContainsKey("SetStation") && (!string.IsNullOrEmpty(tCommands["SetStation"].ToString()))
                    && tCommands.ContainsKey("SetStaStatus") && (tCommands["SetStaStatus"] != null)
                    && tStaServer.getStation(tCommands["SetStation"].ToString(), out tCommunicater))
                {
                    string sStaID = tCommands["SetStation"].ToString();

                    try
                    {
                        Dictionary<string, object> tSetStatus = null;
                        if (tCommands["SetStaStatus"].GetType() == typeof(string))
                        {
                            tSetStatus = JsonConvert.DeserializeObject<Dictionary<string, object>>(tCommands["SetStaStatus"].ToString());
                        }
                        if (tCommands["SetStaStatus"].GetType() == typeof(Dictionary<string, object>))
                        {
                            tSetStatus = (Dictionary<string, object>)tCommands["SetStaStatus"];
                        }
                        //if (tSetStatus != null)
                        ///   tStationTmp.runWithJSON(tCommands, tResponInfos);
                        iResult++;
                    }
                    catch (Exception ex)
                    {
                        SysErr?.setRecord(ex);
                    }
                }
                else
                    foreach (KeyValuePair<string, object> item in tCommands)
                    {
                        int.TryParse(item.Value.ToString(), out iValue);
                        switch (item.Key)
                        {
                            case "GetStationList":

                                //DateTime dtStart = TimeZoneInfo.GetSystemTimeZones().(new DateTime(1970, 1, 1));
                                string[] tOnlineStaIDList = tStaServer.StationsIDList;
                                List<long> tLastActTimeList = new List<long>();
                                tResponInfos.Add("StationIDList", tOnlineStaIDList);
                                tCommunicater = null;
                                //List<int> tStationType = new List<int>();
                                foreach (string StaID in tOnlineStaIDList)
                                {
                                    tCommunicater = tStaServer.getStation(tCommands["StaID"].ToString());
                                    if (tStaServer.getStation(tCommands["StaID"].ToString(),out tCommunicater)
                                        && (tCommunicater.Station != null))
                                    {
                                        tLastActTimeList.Add((tCommunicater.Station.LastActiveTime-DateTime.MinValue).Ticks);
                                        //tStationType.Add(tDestStaTmp.StationType);
                                        tCommunicater = null;
                                    }
                                }
                                tResponInfos.Add("StationLastAct", tLastActTimeList.ToArray());
                                //tResponInfos.Add("StationType", tStationType.ToArray());
                                iResult++;
                                break;
                            case "GetStationStatus":
                                if ((!string.IsNullOrEmpty(item.Value.ToString()))
                                    && tStaServer.getStation(item.Value.ToString(), out tCommunicater)
                                    && (tCommunicater.Station != null))
                                {
                                    //tResponInfos.Add("StationStatus", tCommunicater.Station.getStatus());
                                    iResult++;
                                }
                                break;
                        }
                    }
            }
            //tResponInfos.Add("Result", iResult);
            return iResult > 0;
        }
    }
}
